﻿# Version suffix - use preview suffix for CI builds that are not tagged (i.e. unofficial)
$VersionSuffix = ""
if ($env:APPVEYOR -eq "True" -and $env:APPVEYOR_REPO_TAG -eq "false") {
  $VersionSuffix = "preview-" + $env:APPVEYOR_BUILD_NUMBER.PadLeft(4, '0')
}

# Target folder for build artifacts (e.g. nugets)
$ArtifactsPath = "$(pwd)" + "\artifacts"

function install-package {
  Push-Location src/FFF.Test
  npm set registry https://registry.npm.taobao.org/
  npm install
  Pop-Location
}

function dotnet-build-debug {
  if ($VersionSuffix.Length -gt 0) {
    dotnet build -c Debug --version-suffix $VersionSuffix
  } else {
    dotnet build -c Debug
  }
}

function dotnet-build {
  if ($VersionSuffix.Length -gt 0) {
    dotnet build -c Release --version-suffix $VersionSuffix
  } else {
    dotnet build -c Release
  }
}

function dotnet-pack {
  Get-ChildItem -Path src/** | ForEach-Object {
    if ($VersionSuffix.Length -gt 0) {
      dotnet pack $_ -c Release --no-build -o $ArtifactsPath --version-suffix $VersionSuffix
    } else {
      dotnet pack $_ -c Release --no-build -o $ArtifactsPath
    }
  }
}
function dotnet-run {
  Push-Location src/FFF.Test
  Start-Process -FilePath http://localhost:12068/
  dotnet run  --no-build
  Pop-Location
}

function gen-linqdb {
  Push-Location _commonfile/db
  .\sqlmetal /conn:"server=47.106.165.189;database=Ydm;uid=ydm2;pwd=ydm2123!@#" /dbml:MyDb.dbml
  .\SqlMetal /code:MyDb.designer.cs /map:MyDb.map MyDb.dbml
  Pop-Location
}

@("install-package","dotnet-build-debug","dotnet-run" ) | ForEach-Object {
  echo ""
  echo "***** $_ *****"
  echo ""

  # Invoke function and exit on error
  &$_ 
  if ($LastExitCode -ne 0) { Exit $LastExitCode }
}